/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.engcomp.mpc.domain;

import br.ufrn.engcomp.mpc.Constants;
import br.ufrn.engcomp.mpc.domain.exceptions.PlantCommunicationException;
import br.ufrn.engcomp.mpc.util.MathCommonsUtils;
import org.apache.commons.math.linear.Array2DRowRealMatrix;
import org.apache.commons.math.linear.ArrayRealVector;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.RealVector;

/**
 *
 * @author Felipe
 */
public class CoupledTanksPlantModel extends AbstractCoupledTanksPlant {

    protected RealVector xVector;

    public CoupledTanksPlantModel() {
        super();

        inputOP = new double[] { 0 };
        stateOP = new double[] { 0, 0 };
    }

    @Override
    public void connect(String ip, int port) throws PlantCommunicationException {
        xVector = new ArrayRealVector(Constants.N, 0);
    }

    @Override
    public void shutdown() throws PlantCommunicationException {
        xVector.set(0);
    }

    @Override
    public void applyInput(double u) throws PlantCommunicationException {
        RealMatrix xVectorAsMatrix = MathCommonsUtils.convertColumnVectorToMatrix(xVector);
        RealMatrix uMatrix = new Array2DRowRealMatrix(new double[] { u });

        xVector = MathCommonsUtils.convertMatrixToColumnVector(aMatrix.multiply(xVectorAsMatrix).add(bMatrix.multiply(uMatrix)));
    }

    @Override
    public double[] readOutput() throws PlantCommunicationException {
//        return xVector.add(this.getStateOP()).getData();
        return xVector.getData();
//        return xVector.add(new double[] { 0, 1.5 }).getData();
    }

    @Override
    public String getCodeName() {
        return "LP";
    }
    
}
